#include "SDL_menu.h"
#include <SDL/SDL_image.h>
#include "SDL_rotozoom.h"


void SDL_Menu(SDL_Surface *Ecran)
{
    SDL_menu M;
    initSDL_menu(&M,Ecran);
    boucleSDL_menu(&M,Ecran);
    detruireSDL_menu(&M);
}

void boucleSDL_menu(SDL_menu* S,SDL_Surface *Ecran)
{
    int continuer=1;
    int retboucle=0, jeu=0;
    SDL_Event event;
    collerSurface(Ecran,S->menu,NULL,Ecran->w/2-S->gameover->w/2,0);
    SDL_Flip(Ecran);
    while (continuer)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = 0;
                break;
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym)
                {
                    case SDLK_ESCAPE:
                        continuer = 0;
                        break;
                    case SDLK_RETURN:
                        jeu=1;
                        break;
                    default:
                        break;
                }
                break;
            case SDL_MOUSEBUTTONDOWN:
                if (event.button.x<Ecran->w/4 && event.button.y>5*Ecran->h/6)
                    jeu=1;
                if (event.button.x>3*Ecran->w/4 && event.button.y>4*Ecran->h/5)
                    continuer=0;
                break;
        }
        if (jeu)
        {
            retboucle=SDL_jeu(Ecran);
            if (retboucle==1)
            {
                SDL_FillRect(Ecran,NULL,SDL_MapRGB(Ecran->format,0,0,0));
                collerSurface(Ecran,S->gameover,NULL,Ecran->w/2-S->gameover->w/2,0);
            }
            else
            {
                SDL_FillRect(Ecran,NULL,SDL_MapRGB(Ecran->format,0,0,0));
                collerSurface(Ecran,S->menu,NULL,Ecran->w/2-S->menu->w/2,0);
            }
            SDL_ShowCursor(SDL_ENABLE);
            SDL_Flip(Ecran);
        }
        jeu=0;
    }
    SDL_FillRect(Ecran,NULL,SDL_MapRGB(Ecran->format,255,0,128));
}

void getResvideo(int * x,int * y,int * pleinecran)
{
    FILE* f=fopen("data/config.ini","r");
    int temoin=0;

    if (!f)
    {
        f=fopen("data/config.ini","w");
        SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
        *x=modes[0]->w;
        *y=modes[0]->h;
        *pleinecran|=SDL_FULLSCREEN;
        fprintf(f,"resolutionx=%d\n",modes[0]->w);
        fprintf(f,"resolutiony=%d\n",modes[0]->h);
        fprintf(f,"pleinecran=%d\n",1);

        /* Pour SDL_Joueur */
        fprintf(f,"/*CONTROLES*/\n");
        fprintf(f,"CONFIG=CLAVIER\n");
        fprintf(f,"HAUT=%d\nBAS=%d\nGAUCHE=%d\nDROITE=%d\n"
                ,SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT);
        fprintf(f,"ECHOBJ=%d\nRAMOBJ=%d",SDLK_q,SDLK_e);
        fprintf(f,"\nTIRHAUT=%d\nTIRBAS=%d\nTIRGAUCHE=%d\nTIRDROITE=%d\n"
                ,SDLK_w,SDLK_s,SDLK_a,SDLK_d);
        fprintf(f,"X=%d\nY=%d\nXTIR=%d\nYTIR=%d\n",0,0,0,0);
    }
    else
    {
        fscanf(f,"resolutionx=%d\n",x);
        fscanf(f,"resolutiony=%d\n",y);
        fscanf(f,"pleinecran=%d\n",&temoin);
        if (temoin)
            *pleinecran|=SDL_FULLSCREEN;
    }
    fclose(f);
}

void initSDL_menu(SDL_menu* S,SDL_Surface *Ecran)
{
    SDL_Surface* temp;
    S->menu=IMG_Load("data/menu.png");
    temp=rotozoomSurface( S->menu,0,(float)Ecran->h/(float)S->menu->h,1);
    SDL_FreeSurface(S->menu);
    S->menu=temp;
    S->gameover=IMG_Load("data/gameover.png");
    temp=rotozoomSurface(S->gameover,0,(float)Ecran->h/(float)S->gameover->h,1);
    SDL_FreeSurface(S->gameover);
    S->gameover=temp;
}

void detruireSDL_menu(SDL_menu* S)
{
    SDL_FreeSurface(S->menu);
    SDL_FreeSurface(S->gameover);
}
